#pragma once

#include "parentpanel.h"
#include "tool.h"
#include "toolfactory.h"
#include "imageclip.h"

class ToolBar : public ParentPanel
{
public:
	struct ToolData
	{
		const char * name;
		ImageClip clip;
	};
protected:
	TemplateVector<Tool*> m_bmpTools;
public:
	ToolBar(){}

	void addTools(const int & a_numTools, ToolData * a_tools, void * a_context, ButtonCallbackFunction a_notifyToolUse)
	{
		Tool * tool;
		ImageButton * button;
		for(int i = 0; i < a_numTools; ++i)
		{
			tool = ToolFactory::getNewTool(a_tools[i].name, a_context);
			if(!tool)
			{
				printf("no such tool named \"%s\" in ToolFactory... prepare for explosion!\n", 
					a_tools[i].name);
			}
			tool->setContext(a_context, a_notifyToolUse);
			m_bmpTools.add(tool);
			// TODO fixme... typecasting should not be happening here...
			button = (ImageButton*)tool->getButton();
			button->setButtonImage(a_tools[i].clip);
			addChild(button);
		}
	}
	void setArea(const SRect & a_area)
	{
		SRect buttonArea;
		buttonArea.setPosition(a_area.getPosition());
		Panel * childButton;
		for(int i = 0; i < m_children.size(); ++i)
		{
			childButton = m_children.get(i);
			buttonArea.setDimension(childButton->getArea()->getDimension());
			childButton->setArea(buttonArea);
			buttonArea.setPosition(buttonArea.getPosition().sum(SPoint(buttonArea.getWidth(),0)));
		}
		ParentPanel::setArea(a_area);
	}
	int getToolNamed(const char * a_name, const int & a_startIndex)
	{
		for(int i = a_startIndex; i < m_bmpTools.size(); ++i)
		{
			if(strcmp(a_name, m_bmpTools.get(i)->getName()) == 0)
				return i;
		}
		return -1;
	}
	Tool* getTool(const int & a_index)
	{
		return m_bmpTools.get(a_index);
	}
	void release()
	{
		for(int i = 0; i < m_bmpTools.size(); ++i)
		{
			delete m_bmpTools.get(i);
		}
		m_bmpTools.release();
	}
	~ToolBar(){release();}
};